package com.bjqh.edu.adapter;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.MyPlan;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Utils.DownloadUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/11.
 */

public class PlanAdapter extends BaseAdapter {


    private List<MyPlan.DataBean> list;
    private Context ctx;
    private ViewHolder holder;

    public PlanAdapter(Context ctx, List<MyPlan.DataBean> data) {
        this.ctx = ctx;
        this.list = data;

    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        if (view == null) {
            view = LayoutInflater.from(ctx).inflate(R.layout.item_plan, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tvTitle.setText(list.get(position).getTitle());
        holder.tvContent.setText(list.get(position).getContent());
        holder.tvTime.setText(AppConstants.times(list.get(position).getTime()));
        holder.tvName.setText("---"+list.get(position).getName());
        holder.llXz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("下载图片","下载图片");
            /*    //创建下载任务,downloadUrl就是下载链接
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(AppConstants.IMG_URL+list.get(position).getFilepaht()));
                //指定下载路径和下载文件名
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myplan.jpg");
                //获取下载管理器
                DownloadManager downloadManager= (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
                //将下载任务加入下载队列，否则不会进行下载
                long reference =downloadManager.enqueue(request);*/if (list.get(position).getFilepaht()!=null)
                {
                    DownloadUtil downloadUtil= new DownloadUtil(ctx,AppConstants.IMG_URL+list.get(position).getFilepaht(),"myplan.jpg","图片下载");
                    downloadUtil.download();

                }


            }
        });
        return view;
    }


    static class ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.ll_xz)
        LinearLayout llXz;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
